<?php

require_once dirname(__FILE__).'/../lib/ideaGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/ideaGeneratorHelper.class.php';

/**
 * idea actions.
 *
 * @package    ideas
 * @subpackage idea
 * @author     AlvaroLara
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class ideaActions extends autoIdeaActions
{
  protected $q = null;

  public function getQuery()
  {
    return $this->q;
  }

  public function setQuery($v)
  {
    $this->q = $v;
  }

  public function preExecute ()
  {
    parent::preExecute();

    sfContext::getInstance()->getConfiguration()->loadHelpers('I18N');

    $q = ($this->getRequest()->hasParameter('q'))?trim($this->getRequest()->getParameter('q')):null;

    if ((!empty($q) && ( (is_null($this->getQuery()) || (strcmp( $this->getQuery(), $this->getRequest()->getParameter('q')) != 0) ))))
    {
      $this->setQuery($this->getRequest()->getParameter('q'));
    }
  }

  public function executeFavIdea(sfWebRequest $request)
  {
    $idea = IdeaPeer::retrieveByPk($request->getParameter('id'));

    if($this->getUser()->isAuthenticated())
    {
      $c = new Criteria();
      $c->add(FavouritePeer::USER_ID,$this->getUser()->getAppUser()->getId());
      $c->add(FavouritePeer::IDEA_ID,$idea->getId());
      $count = FavouritePeer::doCount($c);
      if($count > 0)
      {
        $this->getUser()->setFlash('error',__('Seems like you already follow this...'));
        $this->redirect('@idea_show?id='.$idea->getId());
      }
      $fav = new Favourite();
      $fav->setUserId($this->getUser()->getAppUser()->getId());
      $idea->addFavourite($fav);
      $idea->save();
      $this->getUser()->setFlash('error',__('Idea successfully followed! Aint that greate?'));
      $this->redirect('@idea_show?id='.$idea->getId());
    }

    $this->getUser()->setFlash('error',__('You are not registed, if you want to use this. Please register, its free!'));
    $this->redirect('@idea_show?id='.$idea->getId());
  }


  public function buildCriteria()
  {
    $criteria = parent::buildCriteria();

    if(!empty($this->q))
    {
      $criteria = IdeaPeer::searchCriteria($this->getQuery(),$criteria);
    }
    return $criteria;
  }

  protected function getSort()
  {
    $this->setSort($this->configuration->getDefaultSort());

    return $this->getUser()->getAttribute('idea.sort', null, 'admin_module');
  }


  public function executeShow(sfWebRequest $request)
  {
    $this->Idea = IdeaPeer::retrieveByPk($request->getParameter('id'));
    
    $comment = new Comment();

    $comment->setIdea($this->Idea);

    $this->comment_form = new CommentForm($comment);

    $this->helper = new ideaGeneratorHelper();

  }

  public function executeRandomIdea(sfWebRequest $request)
  {
    $this->Idea = IdeaPeer::getRandomIdea();

    $comment = new Comment();

    $comment->setIdea($this->Idea);

    $this->comment_form = new CommentForm($comment);

    $this->helper = new ideaGeneratorHelper();
    
    $this->setTemplate('show');
  }


  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

      $Idea = $form->save();

      $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $Idea)));

      if ($request->hasParameter('_save_and_add'))
      {
        $this->getUser()->setFlash('notice', $notice.' You can add another one below.');

        $this->redirect('@idea_new');
      }
      else
      {
        $this->getUser()->setFlash('notice', $notice);

        $this->redirect('@idea');
      }
    }
    else
    {
      $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
    }
  }

  public function executeNew(sfWebRequest $request)
  {
    if ($this->getUser()->isAuthenticated())
    {
      return parent::executeNew($request);
    }
    $this->getUser()->setFlash('error',__('Only registered users can access this functionality'));
    $this->redirect('idea/index');
  }

  public function executeEdit(sfWebRequest $request)
  {
    if ($this->getUser()->isAuthenticated() && $this->getUser()->getAppUser()->getId() == $this->getRoute()->getObject()->getOwnerId())
    {
      return parent::executeEdit($request);
    }
    else
    {
      $this->getUser()->setFlash('error',__('Only registered users can access this functionality'));
      $this->redirect('idea/show?id='.$this->getRoute()->getObject()->getId());
    }
  }

}
